/**
 * Copyright 2009 - ChunMap
 * 
 * 这个文件是ChunMap项目的一部分。
 * ChunMap是开源软件。
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.orm.obj;

import java.sql.Connection;

import chunmap.orm.DataSession;
import chunmap.orm.DefaultDataSession;
import chunmap.orm.connect.DBUtils;
import chunmap.orm.mapping.Mapping;
import chunmap.orm.mapping.Table;
import chunmap.orm.mapping.column.Pk;
import chunmap.orm.mapping.relation.Many2One;
import chunmap.orm.mapping.type.IntegerType;
import chunmap.orm.mapping.type.StringType;
import chunmap.orm.sql.SqlMakerFactory;

public class DataSessionFactory {

	private static DataSession dataSession;

	public static DataSession getDataSession() {
		if (dataSession == null) {
			createDataSessin();
		}
		return dataSession;
	}

	private static void createDataSessin() {
		Table school = new Table(School.class);
		school.pk(Pk.autoIntegetID("id"));
		school.addColumn("name", new StringType(50));

		Table student = new Table(Student.class);
		student.pk(Pk.autoIntegetID("id"));
		student.addColumn("name", new StringType(40));
		student.addColumn("age", new IntegerType());

		new Many2One(student, "school", school);

		Mapping mapping = new Mapping();
		mapping.register(school, student);

		String driver = "org.sqlite.JDBC";
		String url = "jdbc:sqlite:test.db";
		String userName = "postgres";
		String passWord = "111";
		
		Connection conn = DBUtils.getConnection(driver, url, userName, passWord);
		dataSession = new DefaultDataSession(conn, mapping,SqlMakerFactory.PostgreSql);
	}

}
